home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / WINPROGS.ARJ / CONNECT.C < prev    next >
Text File  |  1990-11-28  |  3KB  |  115 lines

  1. /*   Connect.C   -   Connect the Dots Program
  2.                      Petzold
  3. */
  4.  
  5. #include <windows.h>
  6.  
  7. #define MAXPOINTS 1000
  8.  
  9. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  10.  
  11. int PASCAL WinMain (HANDLE hInstance,
  12.                     HANDLE hPrevInstance,
  13.                     LPSTR  lpszCmdParam,
  14.                     int    nCmdShow)
  15.                     
  16.   {
  17.   static char szAppName[] = "Connect";
  18.   HWND        hwnd;
  19.   MSG         msg;
  20.   WNDCLASS    wndclass;
  21.   
  22.   if (!hPrevInstance)
  23.      {
  24.      wndclass.style            = CS_HREDRAW | CS_VREDRAW;
  25.      wndclass.lpfnWndProc      = WndProc;
  26.      wndclass.cbClsExtra       = 0;
  27.      wndclass.cbWndExtra       = 0;
  28.      wndclass.hInstance        = hInstance;
  29.      wndclass.hIcon            = LoadIcon (NULL, IDI_APPLICATION);
  30.      wndclass.hCursor          = LoadCursor (NULL, IDC_ARROW);
  31.      wndclass.hbrBackground    = GetStockObject (WHITE_BRUSH);
  32.      wndclass.lpszMenuName     = NULL;
  33.      wndclass.lpszClassName    = szAppName;
  34.      
  35.      RegisterClass(&wndclass);
  36.      }
  37.      
  38.   hwnd = CreateWindow (szAppName,
  39.                        "Connect-the-Dots Mouse Demo",
  40.                        WS_OVERLAPPEDWINDOW, 
  41.                        CW_USEDEFAULT,   
  42.                        CW_USEDEFAULT,
  43.                        CW_USEDEFAULT,   
  44.                        CW_USEDEFAULT,   
  45.                        NULL,
  46.                        NULL,
  47.                        hInstance,
  48.                        NULL);
  49.                        
  50.   ShowWindow (hwnd, nCmdShow);
  51.   UpdateWindow (hwnd);
  52.   
  53.   while (GetMessage (&msg, NULL, 0, 0))
  54.     {
  55.     TranslateMessage (&msg);
  56.     DispatchMessage  (&msg);
  57.     }
  58.     
  59.   return msg.wParam;
  60.   }
  61.   
  62. long FAR PASCAL WndProc (HWND hwnd,
  63.                          WORD message,
  64.                          WORD wParam,
  65.                          LONG lParam)
  66.                          
  67.   {
  68.   static POINT points[MAXPOINTS];
  69.   static short nCount;
  70.   HDC          hdc;
  71.   PAINTSTRUCT  ps;
  72.   short        i, j;
  73.     
  74.   switch (message)
  75.     {
  76.     case WM_LBUTTONDOWN:
  77.       nCount = 0;
  78.       InvalidateRect (hwnd, NULL, TRUE);
  79.       return 0;
  80.       
  81.     case WM_MOUSEMOVE:
  82.       if (wParam & MK_LBUTTON && nCount < MAXPOINTS)
  83.         {
  84.         points [nCount++] = MAKEPOINT (lParam);
  85.         hdc = GetDC (hwnd);
  86.         SetPixel (hdc, LOWORD (lParam), HIWORD (lParam), 0l);
  87.         ReleaseDC (hwnd, hdc);
  88.         }
  89.       return 0;
  90.       
  91.     case WM_LBUTTONUP:
  92.       InvalidateRect (hwnd, NULL, FALSE);
  93.       return 0;
  94.       
  95.     case WM_PAINT:
  96.       hdc = BeginPaint (hwnd, &ps);
  97.       
  98.       for (i = 0; i < nCount - 1; i++)
  99.         for (j = i; j < nCount; j++)
  100.           {
  101.           MoveTo (hdc, points[i].x, points[i].y);
  102.           LineTo (hdc, points[j].x, points[j].y);
  103.           }
  104.        EndPaint (hwnd, &ps);
  105.        return 0;
  106.        
  107.     case WM_DESTROY:
  108.       PostQuitMessage (0);
  109.       return 0;
  110.     }
  111.     
  112.   return DefWindowProc (hwnd, message, wParam, lParam);
  113.   }
  114.                   
  115.